package com.guigu.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.MD5;
import com.guigu.common.utils.R;
import com.guigu.userservice.entity.Businessman;
import com.guigu.userservice.entity.Operator;
import com.guigu.userservice.mapper.OperatorMapper;
import com.guigu.userservice.service.BusinessmanService;
import com.guigu.userservice.service.OperatorService;
import com.guigu.userservice.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作员表 服务实现类
 * </p>
 *
 * @author zcy
 * @since 2025-09-27
 */
@Service
public class OperatorServiceImpl extends ServiceImpl<OperatorMapper, Operator> implements OperatorService {

    @Autowired
    private BusinessmanService businessmanService;

    @Override
    public R Login(String username, String password) {
        password= MD5.encrypt(password);
        QueryWrapper<Operator> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",username);

        Operator operator = this.getOne(queryWrapper);
        if (operator == null ) {
            return R.error().message("用户不存在");
        }
        if (operator.getStatus().equals("0"))
            return R.error().message("用户被禁用");

        if (!operator.getPassword().equals(password)) {
            return R.error().message("用户名或密码错误");
        }
        return R.ok().message("登陆成功").data("user",operator);
    }


    @Override
    public R passwordChange(Integer userId, String oldPassword, String newPassword) {
        oldPassword= MD5.encrypt(oldPassword);
        newPassword= MD5.encrypt(newPassword);
        Operator operator = this.getById(userId);
        if (operator == null ) {
            return R.error().message("用户不存在");
        }
        if (!operator.getPassword().equals(oldPassword)) {
            return R.error().message("原密码错误");
        }
        operator.setPassword(newPassword);
        this.updateById(operator);
        return R.ok().message("修改成功");
    }

    @Override
    public R updatePassword(Long id, String password) {
        password= MD5.encrypt(password);
        Operator operator = this.getById(id);
        if (password.equals(operator.getLoginName())){
            return R.error().message("密码不能与用户名相同");
        }
        if (password.equals(operator.getPassword()))
            return R.error().message("新密码不能与旧密码相同");
        operator.setPassword(password);
        this.updateById(operator);
        return R.ok();
    }

    @Override
    public R queryOperatorList(Integer pageNum,Integer pageSize,OperatorVo vo) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Operator> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(vo.getSearchKeyword() != null,"name", vo.getSearchKeyword())
                .or()
                .like(vo.getSearchKeyword() != null,"login_name", vo.getSearchKeyword());
        List<Operator> operators = this.list(queryWrapper);
        PageInfo<Operator> pageInfo = PageInfo.of(operators);
        return R.ok().data("pageInfo", pageInfo);
    }

}
